package com.ruoyi.system.service.impl;

import com.ruoyi.system.domain.PmFaults;
import com.ruoyi.system.mapper.PmFaultsMapper;
import com.ruoyi.system.mapper.PmRepairOrdersMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.WorkordersMapper;
import com.ruoyi.system.service.IndexService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @Title: IndexServiceImpl
 * @Author lqk
 * @Package com.ruoyi.system.service.impl
 * @Date 2024/4/26 11:26
 * @description:
 */
@Service
public class IndexServiceImpl implements IndexService {
    @Resource
    private SysUserMapper userMapper;
    @Resource
    private PmFaultsMapper pmFaultsMapper;
    @Resource
    private WorkordersMapper workordersMapper;
    @Resource
    private PmRepairOrdersMapper pmRepairOrdersMapper;


    @Override
    public Map<String, Object> panelGroup() {
        Map<String, Object> map = new HashMap<>();
        //公司人数总和
        int totalEmployees = userMapper.getTotalEmployees();
        map.put("totalEmployees", totalEmployees);
        //待处理故障信息
        int pendingFaults = pmFaultsMapper.getPendingFaults("1");
        map.put("pendingFaults", pendingFaults);
        //已处理故障信息
        int resolvedFaults = pmFaultsMapper.getPendingFaults("4");
        map.put("resolvedFaults", resolvedFaults);
        //工单信息总和
        int totalWorkorders = workordersMapper.getTotalWorkorders();
        map.put("totalWorkorders", totalWorkorders);
        return map;
    }

    /**
     * @return
     */
    @Override
    public Map<String, Object> getLinChartIndex() {
        Map<String, Object> map = new HashMap<>();
        for (int i = 1; i <= 10; i++) {
            List<String> dataFaults = pmFaultsMapper.count(String.valueOf(i));
            map.put("collection" + i, dataFaults);
        }
        map.put("dateData", getData());

        return map;
    }

    @Override
    public Map<String, Object> getMonIndex() {
        Map<String, Object> map = new HashMap<>();
        for (int i = 1; i <= 10; i++) {
            List<Integer> dataFaults = pmFaultsMapper.countMon(String.valueOf(i));
            map.put("collection" + i, dataFaults);
        }
        return map;
    }

    @Override
    public Map<String, Object> getYearIndex() {
        List<Integer> count = pmFaultsMapper.countYear();
        Map<String, Object> map = new HashMap<>();
        map.put("collection1", count);

        return map;
    }

    private List<String> getData() {
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        LocalDate sevenDaysAgo = currentDate.minusDays(6);
        // 创建一个列表来存储7天的日期字符串
        List<String> dates = new ArrayList<>();

        // 定义日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM-dd");

        // 将当前日期及前6天的日期添加到列表中
        for (int i = 0; i < 7; i++) {
            LocalDate date = sevenDaysAgo.plusDays(i);
            String dateString = date.format(formatter);
            dates.add(dateString);
        }
        return dates;
    }
}
